﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using YJKPresentation.Utils;

namespace YJKMaterialStatistic
{
	public class MEPSetDataToGridParent
	{
		public List<List<int>> GroupingDicData(int nNameIndex, Dictionary<int, List<string>> dicData)
		{
			List<List<int>> list = new List<List<int>>();
			List<int> list2 = new List<int>(dicData.Keys);
			for (int i = 0; i < list2.Count; i++)
			{
				List<int> list3 = new List<int>();
				string b = dicData[list2[i]][nNameIndex];
				list3.Add(list2[i]);
				for (int j = i + 1; j < list2.Count; j++)
				{
					if (dicData[list2[j]][nNameIndex] == b)
					{
						list3.Add(list2[j]);
						list2.Remove(list2[j]);
						j--;
					}
				}
				list.Add(list3);
			}
			return list;
		}

		public void SortData(int nSepcIndex, Dictionary<int, List<string>> dicData, ref List<int> lstKeys)
		{
			if (dicData[lstKeys[0]][0] == "规格")
			{
				return;
			}
			this.ListDataSort(nSepcIndex, dicData, ref lstKeys);
		}

		private void ListDataSort(int nSepcIndex, Dictionary<int, List<string>> dicData, ref List<int> lstKeys)
		{
			if (lstKeys.Count <= 0)
			{
				return;
			}
			List<int> list = new List<int>();
			List<double> lstValue = new List<double>();
			foreach (int num in lstKeys)
			{
				char[] charAryItem = dicData[num][nSepcIndex].ToCharArray();
				double headNumber = this.GetHeadNumber(charAryItem);
				int index = this.ListSort(lstValue, headNumber);
				list.Insert(index, num);
			}
			lstKeys.Clear();
			lstKeys = list;
		}

		private double GetHeadNumber(char[] charAryItem)
		{
			double result = -1.0;
			int i = 0;
			while (i < charAryItem.Length)
			{
				if (!char.IsNumber(charAryItem[i]))
				{
					char[] array = new char[i];
					Array.ConstrainedCopy(charAryItem, 0, array, 0, i);
					string text = new string(array);
					if (text == "")
					{
						result = double.MaxValue;
						break;
					}
					result = Convert.ToDouble(text);
					break;
				}
				else
				{
					i++;
				}
			}
			return result;
		}

		private int ListSort(List<double> lstValue, double dValue)
		{
			int num = lstValue.Count;
			for (int i = 0; i < lstValue.Count; i++)
			{
				if (Geometry.LessThan(dValue, lstValue[i]))
				{
					num = i;
					break;
				}
			}
			lstValue.Insert(num, dValue);
			return num;
		}

		public bool StringIsNumber(string strValue)
		{
			return Regex.IsMatch(strValue, "^\\d+$") || Regex.IsMatch(strValue, "^\\d+\\.\\d+$");
		}
	}
}
